<?php
 class Customers extends CI_Model{
    function __construct(){
        parent::__construct();
    }
    
     /** 
     * Insert data for config
     * @author tien.nguyen
     */
    public function getList(){
        $this->db->select();
        $this->db->from('customers');
        $this->db->join('categories', 'categories.CategoryId = customers.CategoryId', 'left');
        $query = $this->db->get();
        $res = $query->result_array();
        return $res;
    }
    
        
    /**
     * Get product by product id
     * @author tien.nguyen
     */
    public function getCustomer($customerId){
        $query = $this->db->get_where('customers', array('CustomerId' => $customerId));
        return $query->row_array();
    }
    
    /**
     * edit function in config
     * @author tien.nguyen
     */
    public function edit($data, $customerId){
        $data = $this->_convertData($data);
        return $this->db->update('customers', $data, array('CustomerId' => $customerId));
    }
    
    /**
     * Add customer
     * @author tien.nguyen 
     */
    public function add($data){
        return $this->db->insert('customers', $data);
    }
    
    /**
     * Delete customer
     * @author tien.nguyen
     */
    public function delete($customerId){
        return $this->db->delete('customers', array('CustomerId' => $customerId));
    }
    
    /**
     * Convert data array to array of product
     * @author tien.nguyen
     */
    private function _convertData($data){
        $res = array();
        $arr = array('CustomerId', 'CustomerName', 'CustomerAddress', 
            'CustomerPhone', 'Image', 'CustomerDescription', 'CustomerDetail',
            'Visible', 'CategoryId', 'CustomerVideoId');
        foreach($arr as $item){
            if(isset($data[$item])){
                $res[$item] = $data[$item];
            }
        }
        
        //set default value
        $res['Visible'] = isset($res['Visible']) ? $res['Visible'] : 0;
        return $res;
    }
 }
?>